package com.mdoya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdoya.comstant.FeiLeiConstant;
import com.mdoya.domain.Category;
import com.mdoya.exception.BusinessExceptiion;
import com.mdoya.mapper.CategoryMapper;
import com.mdoya.mapper.DishMapper;
import com.mdoya.mapper.SetmealMapper;
import com.mdoya.query.CategoryQuery;
import com.mdoya.service.CategoryService;
import com.mdoya.service.DishService;
import com.mdoya.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author mdoya
 * @version 1.0
 * @description:
 */
@Scope("singleton")
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    // 通过构造器注入对象
    private DishService dishService;
    private SetmealService setmealService;

    public CategoryServiceImpl(DishService dish, SetmealService setmeal) {
        this.dishService = dish;
        this.setmealService = setmeal;
    }

    @Override
    public Integer insert(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public Integer deleteById(Long categoryId, Integer type) {
        // 类型 1 菜品分类dish表    2 套餐分类setmeal表
        Integer count = 0;
        if (type == FeiLeiConstant.FEILEI_TYPE_ONE) {
            count = dishService.selectCategoryById(categoryId);
            if (count > 0) {
                throw new BusinessExceptiion(-1, "菜品分类有关联商品，禁止删除!");
            }
        } else {
            count = setmealService.selectCategoryById(categoryId);
            if (count > 0) {
                throw new BusinessExceptiion(-1, "套餐有关联商品，禁止删除!");
            }
        }
        return categoryMapper.deleteById(categoryId);
    }

    @Override
    public Integer updateById(Category category) {
        return categoryMapper.updateById(category);
    }

    @Override
    public IPage<Category> selectPage(CategoryQuery categoryQuery) {
        IPage<Category> page = new Page<>(categoryQuery.getPage(), categoryQuery.getPageSize());
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort);
        return categoryMapper.selectPage(page, null);
    }

    /**
     * 根据type类型查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> selectTypeCategoryList(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type != null, Category::getType, type);
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        return categoryMapper.selectList(queryWrapper);
    }

    /**
     * @param id 对应表的字段
     * @return
     */
    @Override
    public Category selectTypeCategory(Long id) {
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getId, id);
        return categoryMapper.selectOne(categoryLambdaQueryWrapper);
    }

    /**
     * 首页查询所有分类类型
     *
     * @return
     */
//    @Override
//    public List<Category> findTypeAll() {
//        return categoryMapper.selectList(null);
//    }
}
